package ltd.smallwang.app.task;

import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ltd.smallwang.app.task.convert.TaskConvert;
import ltd.smallwang.client.task.api.TaskService;
import ltd.smallwang.client.task.dto.TaskAddCmd;
import ltd.smallwang.client.task.dto.TaskCompleteChangeCmd;
import ltd.smallwang.client.task.dto.TaskDto;
import ltd.smallwang.client.task.dto.TaskQry;
import ltd.smallwang.domain.task.Task;
import ltd.smallwang.domain.task.constant.CompleteFlagEnum;
import ltd.smallwang.domain.task.gateway.TaskGateway;
import ltd.smallwang.infrastructure.exception.TaskErrorCode;
import ltd.smallwang.infrastructure.task.db.TaskMapper;
import ltd.smallwang.infrastructure.task.db.model.TaskPo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author justd
 * @description 功能描述
 * @since 2024-04-14 23:06
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class TaskServiceImpl implements TaskService {
  private final TaskGateway taskGateway;

  private final TaskMapper taskMapper;

  @Transactional
  @Override
  public void createTask(TaskAddCmd taskAddCmd) {
    Task task = new Task();
    task.setName(taskAddCmd.getName());
    task.setDescription(taskAddCmd.getDescription());
    // TODO: 用户信息从会话中获取
    task.setStartTime(taskAddCmd.getStartTime());
    task.setEndTime(taskAddCmd.getEndTime());
    task.setCompleteFlagEnum(CompleteFlagEnum.NOT_COMPLETE);
    taskGateway.save(task);
  }

  @Override
  public List<TaskDto> queryTasks(TaskQry taskQry) {
    List<TaskPo> taskPoList = new ArrayList<>();
    if (taskQry.getStartTime() != null && taskQry.getEndTime() != null) {
      taskPoList = taskMapper.queryTasksByStartTimeAndEndTime(taskQry.getStartTime(), taskQry.getEndTime());
    }
    if (taskQry.getStartTime() != null && taskQry.getEndTime() == null) {
      taskPoList = taskMapper.queryTasksByStartTim(taskQry.getStartTime());
    }
    if (taskQry.getStartTime() == null && taskQry.getEndTime() == null) {
      taskPoList = taskMapper.selectList(null);
    }
    return taskPoList.stream().map(TaskConvert.INSTANCE::convert).toList();
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void taskCompleteChange(TaskCompleteChangeCmd taskCompleteChangeCmd) {
    QueryWrapper<TaskPo> queryWrapper = new QueryWrapper<>();
    queryWrapper.lambda().eq(TaskPo::getId, taskCompleteChangeCmd.getId());
    TaskPo taskPo = taskMapper.selectOne(queryWrapper);
    if (taskPo == null) {
      throw new BizException(TaskErrorCode.TASK_NOT_FOUND.getErrorCode(), TaskErrorCode.TASK_NOT_FOUND.getErrorMessage());
    }
    taskPo.setCompleteFlag(taskPo.getReverseCompleteFlag());
    if (taskPo.checkComplete()) {
      taskPo.setCompleteTime(LocalDateTime.now());
    }
    taskMapper.updateById(taskPo);
    if (!taskPo.checkComplete()) {
      taskMapper.clearCompleteTime(taskPo.getId());
    }
  }
}
